﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using SAAC.DTO;
using System.Data;


namespace SAAC.DAL
{
	public class EstagioDAL : DALBase
	{

		public EstagioDTO Select(EstagioDTO dto, string order = null)
		{
			try
			{
				string sql = "CALL sp_estagio_sel(@id,@planoEstagio,@termoCompromisso,@relatorio,@order,@limit);";

				if (dto.PlanoEstagio == null) dto.PlanoEstagio = new AnexoDTO();
				if (dto.TermoCompromisso == null) dto.TermoCompromisso = new AnexoDTO();
				if (dto.Relatorio == null) dto.Relatorio = new AnexoDTO();

				MySqlCommand cmd = new MySqlCommand(sql, con);
				cmd.Parameters.AddWithValue("@id", dto.Id);
				cmd.Parameters.AddWithValue("@planoEstagio", dto.PlanoEstagio.Id);
				cmd.Parameters.AddWithValue("@termoCompromisso", dto.TermoCompromisso.Id);
				cmd.Parameters.AddWithValue("@relatorio", dto.Relatorio.Id);
				cmd.Parameters.AddWithValue("@order", order);
				cmd.Parameters.AddWithValue("@limit", 1);

				con.Open();
				MySqlDataReader dr;

				dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

				if (dr.Read())
				{
					dto.Id = Convert.ToInt32(dr["matricula"]);
					dto.PlanoEstagio.Id = Convert.ToInt32(dr["planoEstagio"]);
					dto.TermoCompromisso.Id = Convert.ToInt32(dr["termoCompromisso"]);
					dto.Relatorio.Id = Convert.ToInt32(dr["relatorio"]);
				}
				else dto = null;

				return dto;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

		public List<EstagioDTO> SelectAll(EstagioDTO dto, string order = null, int limit = 1000)
		{
			try
			{
				string sql = "CALL sp_estagio_sel(@id,@planoEstagio,@termoCompromisso,@relatorio,@order,@limit);";

				if (dto.PlanoEstagio == null) dto.PlanoEstagio = new AnexoDTO();
				if (dto.TermoCompromisso == null) dto.TermoCompromisso = new AnexoDTO();
				if (dto.Relatorio == null) dto.Relatorio = new AnexoDTO();
				var lista = new List<EstagioDTO>();

				MySqlCommand cmd = new MySqlCommand(sql, con);
				cmd.Parameters.AddWithValue("@id", dto.Id);
				cmd.Parameters.AddWithValue("@planoEstagio", dto.PlanoEstagio.Id);
				cmd.Parameters.AddWithValue("@termoCompromisso", dto.TermoCompromisso.Id);
				cmd.Parameters.AddWithValue("@relatorio", dto.Relatorio.Id);
				cmd.Parameters.AddWithValue("@order", order);
				cmd.Parameters.AddWithValue("@limit", limit);

				con.Open();
				MySqlDataReader dr;

				dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

				while (dr.Read())
				{
					var item = new EstagioDTO();
					item.Id = Convert.ToInt32(dr["matricula"]);
					item.PlanoEstagio.Id = Convert.ToInt32(dr["planoEstagio"]);
					item.TermoCompromisso.Id = Convert.ToInt32(dr["termoCompromisso"]);
					item.Relatorio.Id = Convert.ToInt32(dr["relatorio"]);
					lista.Add(item);
				}

				return lista;
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}


		public void Insert(EstagioDTO dto)
		{
			try
			{
				String sql = @"CALL sp_estagio_ins(@id,@planoEstagio,@termoCompromisso,@relatorio);";

				MySqlCommand cmd = new MySqlCommand(sql, con);

				cmd.Parameters.AddWithValue("@id", dto.Id);
				cmd.Parameters.AddWithValue("@planoEstagio", dto.PlanoEstagio.Id);
				cmd.Parameters.AddWithValue("@termoCompromisso", dto.TermoCompromisso.Id);
				cmd.Parameters.AddWithValue("@relatorio", dto.Relatorio.Id);

				con.Open();
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

		public void Update(EstagioDTO dto)
		{
			try
			{
				String sql = "CALL sp_estagio_upd(@id,@planoEstagio,@termoCompromisso,@relatorio);";

				MySqlCommand cmd = new MySqlCommand(sql, con);

				cmd.Parameters.AddWithValue("@id", dto.Id);
				cmd.Parameters.AddWithValue("@planoEstagio", dto.PlanoEstagio.Id);
				cmd.Parameters.AddWithValue("@termoCompromisso", dto.TermoCompromisso.Id);
				cmd.Parameters.AddWithValue("@relatorio", dto.Relatorio.Id);

				con.Open();
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}

		public void Delete(EstagioDTO dto)
		{
			try
			{
				String sql = "CALL sp_estagio_del(@id);";

				MySqlCommand cmd = new MySqlCommand(sql, con);

				cmd.Parameters.AddWithValue("@id", dto.Id);
				con.Open();
				cmd.ExecuteNonQuery();
			}
			catch (Exception ex)
			{
				throw ex;
			}
			finally
			{
				con.Close();
			}
		}
	}
}
